<?php
function edit_bckbimbel($config,$param){
	$desc = trim($param['descbimbel']);
	if ($desc!=""){
		$db = new db($config);
		$que = "UPDATE `deskripsi` SET `deskripsi`='$desc' WHERE `page` = 'bimbel';";
		$db->exec($que);
		$_SESSION['editted'] = 'Sukses edit deskripsi Bimbel';
	}
	header('Location: '.$config['home_url']."backend/bimbel");
}

function add_bckbimbelfoto($config,$param){
	$uploaddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'slide'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'pre'.DIRECTORY_SEPARATOR;
	$limitedext = array("jpg","jpeg","png","bmp");
	$ext = strrchr($_FILES['gambar']['name'],'.');
	$ext = strtolower($ext);
	$getExt = explode('.',$ext);
	$getName = explode('.',$_FILES['gambar']['name']);
	$getName = helper::slugging($getName[0]);
	$file_ext = $getExt[count($getExt)-1];
	$file_name = $getName.".".$file_ext;
	$uploadfile = $uploaddir.$file_name;
	if (!in_array($file_ext,$limitedext)) {
		$_SESSION['editted'] = "Tipe file salah<br />Tipe file yang diperbolehkan adalah jpg, jpeg, bmp, png.";
	} else {
		echo $_FILES['gambar']['tmp_name'];
		echo "\n".$uploadfile;
		if (move_uploaded_file($_FILES['gambar']['tmp_name'], $uploadfile)) {
			// process for thumbnail image
			//$file_name = $_FILES['gambar']['name'];
			echo "\n".$file_name;
			WideImage::load($uploadfile)->resize(790,250,'fill')->saveToFile($uploadfile);	//slide
			WideImage::load($uploadfile)->resize(154,50,'fill')->saveToFile($preddir.$file_name);	//pre
			$_SESSION['editted'] = "Sukses upload gambar";
		} else {
			switch ($_FILES['gambar']['error']) {
				case 2:
					$_SESSION['editted'] = "Error: Ukuran gambar melebihi batas<br />Batas ukuran file adalah 1 MB";
					break;
				case 7:
					$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
					break;
				default:
					$_SESSION['editted'] = "Error: Unknown error ".$_FILES['gambar']['error'];
			}
		}
	}
	//die();
	header('Location: '.$config['home_url']."backend/bimbel");
}

function del_bckbimbelfoto($config,$param){
	$img=$param['img'];
	$uploaddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'slide'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'pre'.DIRECTORY_SEPARATOR;
	unlink($uploaddir.$img);
	unlink($preddir.$img);
	$_SESSION['editted'] = 'Sukses hapus foto';
	header('Location: '.$config['home_url']."backend/bimbel");
}

function edit_bckbimbelkontak($config,$param){
	$notelp = trim($param['notelp']);
	$email = trim($param['email']);
	$alamat = trim($param['alamat']);
	var_dump($param);
	$db = new db($config);
	$que = "UPDATE `kontak` SET `telp`=\"$notelp\", `email`=\"$email\", `alamat`=\"$alamat\" WHERE `page` = 'bimbel';";
	$db->exec($que);
	$_SESSION['editted'] = 'Sukses edit kontak';
	header('Location: '.$config['home_url']."backend/bimbel");
}

function add_bckbimbelkeg($config,$param){
	$keg = trim($param['kegplus']);
	$slug = helper::slugging($keg);
	$desc = trim($param['descplus']);
	if ($keg!=""){
		$db = new db($config);
		$que = "INSERT INTO `bimbel` (`kegiatan` ,`slug-kegiatan` ,`deskripsi`) VALUES (\"$keg\", \"$slug\", \"$desc\");";
		$db->exec($que);
		if ($db->lastInsertId()>0)
			$_SESSION['editted'] = 'Sukses tambah kegiatan';
		else
			$_SESSION['editted'] = 'Gagal tambah kegiatan';
	}
	header('Location: '.$config['home_url']."backend/bimbel");
}

function add_bckbimbelsub($config,$param){
	$sub = trim($param['subplus']);
	$slug = helper::slugging($sub);
	$desc = trim($param['descplus']);
	$idk = $param['idKeg'];
	if ($sub!=""){
		$db = new db($config);
		$que = "INSERT INTO `sub_bimbel` (`idBimbel`, `subb`, `slug-subb` ,`desc`) VALUES ($idk, \"$sub\", \"$slug\", \"$desc\");";
		//echo $que; die();
		$db->exec($que);
		if ($db->lastInsertId()>0)
			$_SESSION['editted'] = 'Sukses tambah sub kegiatan : '.$sub;
		else
			$_SESSION['editted'] = 'Gagal tambah sub kegiatan : '.$sub;
	}
	header('Location: '.$config['home_url']."backend/bimbel");
}

function edit_bckbimbelkeg($config,$param){
	$keg = trim($param['keged']);
	$slug = helper::slugging($keg);
	$desc = trim($param['desced']);
	$idk = $param['id'];
	if ($keg!=""){
		$db = new db($config);
		$que = "UPDATE `bimbel` SET `kegiatan`=\"$keg\", `slug-kegiatan`=\"$slug\", `deskripsi`=\"$desc\" WHERE `idBimbel` = $idk;";
		echo $que;
		$db->exec($que);
		$_SESSION['editted'] = 'Sukses edit kegiatan';
	}
	header('Location: '.$config['home_url']."backend/bimbel");
}

function edit_bckbimbelsub($config,$param){
	$sub = trim($param['subed']);
	$slug = helper::slugging($sub);
	$desc = trim($param['desced']);
	$ids = $param['id'];
	if ($sub!=""){
		$db = new db($config);
		$que = "UPDATE `sub_bimbel` SET `subb`=\"$sub\", `slug-subb`=\"$slug\", `desc`=\"$desc\" WHERE `idSubbimbel` = $ids;";
		echo $que;
		$db->exec($que);
		$_SESSION['editted'] = 'Sukses edit sub kegiatan : '.$sub;
	}
	header('Location: '.$config['home_url']."backend/bimbel");
}

function add_bckbimbelkegfoto($config,$param){
	$uploaddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'foto'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'pre-foto'.DIRECTORY_SEPARATOR;
	$idk = $param['idk'];
	$ids = $param['ids'];
	$limitedext = array("jpg","jpeg","png","bmp");
	$ext = strrchr($_FILES['gambar']['name'],'.');
	$ext = strtolower($ext);
	$getExt = explode('.',$ext);
	$getName = explode('.',$_FILES['gambar']['name']);
	$getName = helper::slugging($getName[0]);
	$file_ext = $getExt[count($getExt)-1];
	$file_name = $idk."-".$ids."-".$getName.".".$file_ext;
	$uploadfile = $uploaddir.$file_name;
	if (!in_array($file_ext,$limitedext)) {
		$_SESSION['editted'] = "Tipe file salah<br />Tipe file yang diperbolehkan adalah jpg, jpeg, bmp, png.";
	} else {
		if (move_uploaded_file($_FILES['gambar']['tmp_name'], $uploadfile)) {
			// process for thumbnail image
			WideImage::load($uploadfile)->resize(500,300,'fill')->saveToFile($uploadfile);	//works
			WideImage::load($uploadfile)->resize(100,60,'fill')->saveToFile($preddir.$file_name);	//pre
			$_SESSION['editted'] = "Sukses upload gambar";
		} else {
			switch ($_FILES['gambar']['error']) {
				case 2:
					$_SESSION['editted'] = "Error: Ukuran gambar melebihi batas<br />Batas ukuran file adalah 1 MB";
					break;
				case 7:
					$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
					break;
				default:
					$_SESSION['editted'] = "Error: Unknown error ".$_FILES['gambar']['error'];
			}
		}
	}
	header('Location: '.$config['home_url']."backend/bimbel");
}

function del_bckbimbelkegfoto($config,$param){
	$img=$param['img'];
	$uploaddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'foto'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'pre-foto'.DIRECTORY_SEPARATOR;
	unlink($uploaddir.$img);
	unlink($preddir.$img);
	$_SESSION['editted'] = 'Sukses hapus foto';
	header('Location: '.$config['home_url']."backend/bimbel");
}

function del_bckbimbelkeg($config){
	$idk = $config['ruri'][2];
	//hapus db
	$db = new db($config);
	$que = "DELETE FROM bimbel WHERE idBimbel=$idk";
	echo $que;
	$db->exec($que);
	//hapus gambar
	$uploaddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'foto'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'pre-foto'.DIRECTORY_SEPARATOR;
	$regex = $uploaddir.$idk."-*";
	$limitedext = array("jpg","jpeg","png","bmp");
	//FILTER
	foreach (glob($regex) as $files) {
		$getFile = explode(DIRECTORY_SEPARATOR,$files);
		$file = $getFile[count($getFile)-1];
		$getExt = explode('.',$file);
		$file_ext = $getExt[count($getExt)-1];
		if (in_array($file_ext,$limitedext)) {
			unlink($uploaddir.$file);
			unlink($preddir.$file);
		}
	}
	$_SESSION['editted'] = 'Sukses hapus kegiatan';
	header('Location: '.$config['home_url']."backend/bimbel");
}

function del_bckbimbelsub($config){
	$idk = $config['ruri'][2];
	$ids = $config['ruri'][3];
	//hapus db
	$db = new db($config);
	$que = "DELETE FROM sub_bimbel WHERE idSubbimbel=$ids";
	echo $que;
	$db->exec($que);
	//hapus gambar
	$uploaddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'foto'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR.'pre-foto'.DIRECTORY_SEPARATOR;
	$regex = $uploaddir.$idk."-".$ids."-*";
	$limitedext = array("jpg","jpeg","png","bmp");
	//FILTER
	foreach (glob($regex) as $files) {
		$getFile = explode(DIRECTORY_SEPARATOR,$files);
		$file = $getFile[count($getFile)-1];
		$getExt = explode('.',$file);
		$file_ext = $getExt[count($getExt)-1];
		if (in_array($file_ext,$limitedext)) {
			unlink($uploaddir.$file);
			unlink($preddir.$file);
		}
	}
	$_SESSION['editted'] = 'Sukses hapus sub kegiatan';
	header('Location: '.$config['home_url']."backend/bimbel");
}

function edit_bckbimbeldl($config,$param){
	$uploaddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR;
	$ext = strrchr($_FILES['unduh']['name'],'.');
	$ext = strtolower($ext);
	$getExt = explode('.',$ext);
	$file_ext = $getExt[count($getExt)-1];
	$file_name = "download.".$file_ext;
	$uploadfile = $uploaddir.$file_name;
	if (move_uploaded_file($_FILES['unduh']['tmp_name'], $uploadfile)) {
		$_SESSION['editted'] = "Sukses upload file";
	} else {
		switch ($_FILES['unduh']['error']) {
			case 2:
				$_SESSION['editted'] = "Error: Ukuran file melebihi batas<br />Batas ukuran file adalah 1 MB";
				break;
			case 7:
				$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
				break;
			default:
				$_SESSION['editted'] = "Error: Unknown error ".$_FILES['unduh']['error'];
		}
	}
	header('Location: '.$config['home_url']."backend/bimbel");
}

function del_bckbimbeldl($config,$param){
	$file=$param['file'];
	$uploaddir = $config['image_path'].'bimbel'.DIRECTORY_SEPARATOR;
	unlink($uploaddir.$file);
	$_SESSION['editted'] = 'Sukses hapus file';
	header('Location: '.$config['home_url']."backend/bimbel");
}
